package com.ag.lzw.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

/**时间处理类
 * */
public class DateHandeler {

	private static final Logger logger = Logger.getLogger(DateHandeler.class);
	
	private static SimpleDateFormat sdf;
	
	DateHandeler(){
		
	}
	
	/**字符串转换成Date类型的方法
	 * */ 
	@SuppressWarnings("finally")
	public static Date stringToDate(String strTime) {
		Date d = null;
		sdf = getDateFormateInstance();
		try {
			d = sdf.parse(strTime);
		} catch (ParseException e) {
			logger.debug(e);
		} finally {
			return d;
		}
	}

	public static String DateToString(Date d) {
		sdf = getDateFormateInstance();
		return sdf.format(d);
	}

	/**@return sdf SimpleDateFormat
	 * 返回一个SimpleDateFormat的实例
	 * */
	public static synchronized SimpleDateFormat getDateFormateInstance() {
		if (sdf == null) {
			logger.info("------ New SimpleDateFormat Instance ------");
			sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		}
		return sdf;
	}
}
